import BasicController from './../BasicController.js'
import TradingModel from './../../model/trading/tradingModel.js'
import ErrorHandlerCode from './../../util/ErrorHandlerCode.js'
 
class TradingController extends BasicController{
	insert(req,res){
    	if(!super.isEmptyParameter(req.body.goods_host)){
    		res.json({
                code:ErrorHandlerCode.MISSINGPARAMETER,
                message: 'goods_host 为必填参数'
            })
            return;
    	}
    	if(!super.isEmptyParameter(req.body.goods_name)){
    		res.json({
                code:ErrorHandlerCode.MISSINGPARAMETER,
                message: 'goods_name 为必填参数'
            })
            return;
    	}
    	if(!super.isEmptyParameter(req.body.goods_num)){
    		res.json({
                code:ErrorHandlerCode.MISSINGPARAMETER,
                message: 'goods_num 为必填参数'
            })
            return;
    	}
    	if(!super.isEmptyParameter(req.body.goods_price)){
    		res.json({
                code:ErrorHandlerCode.MISSINGPARAMETER,
                message: 'goods_price 为必填参数'
            })
            return;
    	}
    	if(!super.isEmptyParameter(req.body.goods_remark)){
    		res.json({
                code:ErrorHandlerCode.MISSINGPARAMETER,
                message: 'goods_remark 为必填参数'
            })
            return;
    	}
    	if(!super.isEmptyParameter(req.body.goods_picture)){
    		res.json({
                code:ErrorHandlerCode.MISSINGPARAMETER,
                message: 'goods_picture 为必填参数'
            })
            return;
    	}
    	TradingModel.insert(req.body).then((data) =>{
            super.RequestSuccessMsg(res, data);
        }).catch((err) => {
            res.json(ErrorHandlerCode.INNERSERVICERROR);
            throw err;
        })
	}
	delFoods(req,res){
		if(!super.isEmptyParameter(req.body.id)){
    		res.json({
                code:ErrorHandlerCode.MISSINGPARAMETER,
                message: 'id 为必填参数'
            })
            return;
    	}
    	TradingModel.findById(req.body.id).then((data)=>{
    		if(data.goods_status === 1){
    			res.json({
	                code:321,
	                message: '该物品已经出售无法删除'
            	})
    		}else{
    			TradingModel.del(req.body.id).then((data)=>{
    				super.RequestSuccessMsg(res,data);
    			});
    		}
    	}).catch((err) => {
            res.json(ErrorHandlerCode.INNERSERVICERROR);
            throw err;
        });
	}
	findListAll(req,res){
		TradingModel.findAll().then((data)=>{
			const resData = {
				id: data.id,
			    goods_host:data.goods_host,
			    goods_name:data.goods_name,
			    goods_num:data.goods_num,
			    goods_price:data.goods_price,
			    goods_remark:data.goods_remark,
			    goods_picture:data.goods_price,
			    goods_status:data.goods_status,
			    created_at:data.created_at
			}
			super.RequestSuccessMsg(res, resData);
		}).catch((err) => {
            res.json(ErrorHandlerCode.INNERSERVICERROR);
            throw err;
        });
	}
	findListPage(req,res){
		if(!super.isEmptyParameter(req.body.page_size)){
			req.body.page_size = 10;
		}
		if(!super.isEmptyParameter(req.body.page_no)){
			req.body.page_no = 1;
		}
		TradingModel.findListPage(req.body).then((data)=>{
			super.RequestSuccessMsg(res, data);
		});
	}
	update(req,res){
		if(!super.isEmptyParameter(req.body.id)){
			res.json({
                code:ErrorHandlerCode.MISSINGPARAMETER,
                message: 'id 为必填参数'
            })
            return;
		}
		TradingModel.update(req.body).then((datr)=>{
			super.RequestSuccessMsg(res);
		});
	}
	sellFoods(req,res){
		if(!super.isEmptyParameter(req.body.id)){
			res.json({
                code:ErrorHandlerCode.MISSINGPARAMETER,
                message: 'id 为必填参数'
            })
            return;
		}
		const reqData = {
			id:req.body.id,
			goods_status:1
		}
		TradingModel.update(reqData).then((data)=>{
			super.RequestSuccessMsg(res);
		});
	}
}
module.exports = new TradingController();